package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.Jedis;

import java.io.Serializable;
import java.util.UUID;

/**
 * @author lv
 * @date 2021/6/12 22:35
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {
    private Logger logger = LoggerFactory.getLogger(RedisTest.class);
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private RedisLock redisLock;
    @Autowired
    private Counter counter;
    @Test
    public void test() {
        /*String key = "user:1";
        redisTemplate.opsForValue().set(key, new User(1,"pjmike",20));
        User user = (User) redisTemplate.opsForValue().get(key);
        logger.info("uesr: "+user.toString());
        System.out.println("uesr: "+user.toString());
        boolean b = redisLock.lock("12345678");
        System.out.println(b);
        System.out.println(redisTemplate.opsForValue().get("redis_lock"));
        redisLock.unlock("12345678");
        System.out.println(redisTemplate.opsForValue().get("redis_lock"));*/
        try {
            redisTemplate.opsForValue().set("iphone12-num",12);
            int num = counter.reduce("iphone12-num");
            System.out.println(num);
            System.out.println(redisTemplate.opsForValue().get("redis_lock"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String orderId = UUID.randomUUID().toString();
        redisTemplate.convertAndSend("order:test1",orderId);
        String orderId2 = UUID.randomUUID().toString();
        redisTemplate.convertAndSend("order:test2",orderId2);
    }

}
